<?php

namespace App\Http\Controllers\Api\Admin;

use App\Http\Controllers\Api\Controller;
use App\Http\Requests\Api\AdminRequest;
use App\Http\Resources\Api\AdminResource;
use App\Models\Admin;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Mews\Captcha\Facades\Captcha;

class AdminController extends Controller
{

    //返回用户列表
    public function index(Request $request)
    {
        $admins = Admin::getList($request);
        return $this->success($admins);
    }
    //返回单一用户信息
    public function show($id)
    {
        $userInfo = Admin::getInfo($id);
        return $this->success($userInfo);
    }
    //用户注册
    public function store(AdminRequest $request)
    {
        // 判断名称是否存在
        $userInfo = DB::table('admins')->where('name', $request->name)->first();
        if ($userInfo && $userInfo->id) {
            return $this->failed('用户名已注册');
        }
        Admin::create($request->all());
        return $this->setStatusCode(201)->success('用户注册成功');
    }
    //用户登录
    public function login(Request $request)
    {
        // var_dump($aaa);exit;
        // print_r($aaa);exit;
        // 校验验证码
        if (($request->code && !Captcha::check_api($request->code, $request->ckey, 'flat')) || !$request->code) {
          return $this->success(['msg' => '验证码错误'], 'failed');
        } 
        // 获取当前守护的名称
        $present_guard = Auth::getDefaultDriver();
        $token = Auth::claims(['guard' => $present_guard])->attempt(['name' => $request->name, 'password' => $request->password]);
        if ($token) {
            // 查询用户角色信息
            $userInfo = DB::table('admins')->select('id', 'name', 'status', 'role_id', 'created_at')->where('name', $request->name)->first();
            if ($userInfo && $userInfo->id) {
                // 权限相关
                $userInfo->roles = $userInfo->name === 'admin' ? ['admin'] : ['common'];
                $userInfo->authBtnList = [];
                return $this->setStatusCode(201)->success(['token' => 'bearer ' . $token]);
            } else {
                return $this->success(['msg' => '当前账号不存在'], 'failed');
            }
        }
        return $this->success(['msg' => '账号或密码错误'], 'failed');
    }

    //用户退出
    public function logout()
    {
        Auth::logout();
        return $this->success('退出成功...');
    }
    //返回当前登录用户信息
    public function info()
    {
        $admin = Auth::user();
        return $this->success(new AdminResource($admin));
    }

    // 用户信息
    public function detail(Request $reques)
    {
        $admin = Admin::getInfoByName($reques->name);
        // 权限相关
        $admin['userName'] = $admin['name'];
        $admin['roles'] = $admin['name'] === 'admin' ? ['admin'] : ['common'];
        $admin['authBtnList'] = [];
        return $this->success($admin);
    }

    // 修改用户
    public function update(Request $request)
    {
        $userInfo = DB::table('admins')->where('id', $request->id)->first();
        if ($userInfo && $userInfo->id) {
            $updateData = [
                'name' => $request->name ?? $userInfo->name,
                'password' => $request->password ? bcrypt($request->password) : $userInfo->password,
                'status' => $request->status ?? $userInfo->status,
                'role_id' => $request->role_id ?? $userInfo->role_id,
                'updated_at' => date('Y-m-d H:i:s'),
            ];
            $result = DB::table('admins')->where('id', $request->id)->update($updateData);
            if ($result) {
                return $this->success();
            }
            return $this->failed('修改失败');
        } else {
            return $this->failed('用户不存在');
        }
    }

    // 删除用户
    public function delete($id)
    {
        $result = DB::table('admins')->where('id', $id)->delete();
        if ($result) {
            return $this->success();
        }
        return $this->failed('删除失败');
    }
}
